package com.yanqu.road.server.manger;

import com.yanqu.road.entity.enums.eUpdateScope;
import com.yanqu.road.entity.servercenter.PackageMark;
import com.yanqu.road.entity.servercenter.PackageMjbContent;
import com.yanqu.road.entity.servercenter.PackageUpdateInfo;
import com.yanqu.road.entity.servercenter.PackageVersion;
import com.yanqu.road.logic.bussiness.servercenter.ChannelBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PackageVersionMgr extends TempMgr {

	private static final Logger logger = LogManager.getLogger(PackageVersionMgr.class.getName());

	private static List<PackageMark> packageMarkList;
	private static Map<String, PackageMark> packageMarkMap;

	private static List<PackageVersion> packageVersionList;
	private static Map<String, Map<String, PackageVersion>> packageVersionMap;
	private static Map<String, PackageVersion> recommendPackageVersionMap;
	private static Map<String, PackageVersion> whiteRecommendPackageVersionMap;         //白名单推荐版本
	private static Map<String, PackageVersion> forwardRecommendPackageVersionMap;       //先遣服推荐版本

	//新逻辑
	private static List<PackageUpdateInfo> packageUpdateInfoList;
	private static Map<String, List<PackageUpdateInfo>> packageUpdateInfoMap;

	private static List<PackageMjbContent> packageContentList;							// 马甲包文本专用

	@Override
	public boolean stop() {
		return false;
	}

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean reloadData() {
		packageMarkList = ChannelBussiness.getPackageMarkList();
		packageMarkMap = initPackageMarkMap();

		packageVersionList = ChannelBussiness.getPackageVersionList();
		packageContentList = ChannelBussiness.getPackageContentList();
		initRecommendVersionMap();

		//新逻辑
		packageUpdateInfoList = ChannelBussiness.getPackageUpdateInfoList();
		packageContentList = ChannelBussiness.getPackageContentList();
		initPackageUpdateVersionMap();
		logger.info("=========reload PackageVersionMgr");
		return true;
	}

	@Override
	public boolean reloadConfig() throws Exception {
		return true;
	}

	public static List<PackageMjbContent> getPackageContentList() {
		return packageContentList;
	}

	public static PackageMjbContent getPackageContent(String mark, boolean isReview) {
		for (PackageMjbContent packageMjbContent : packageContentList) {
			if (packageMjbContent.getPackageMark().equals(mark) && isReview == packageMjbContent.isReview()) {
				return packageMjbContent;
			}
		}

		return null;
	}

	/**
	 * 初始化包体标识集合
	 * @return
	 */
	private Map<String, PackageMark> initPackageMarkMap(){
		Map<String, PackageMark> dataMap = new ConcurrentHashMap<>();
		for(PackageMark packageMark : packageMarkList){
			dataMap.put(packageMark.getPackageMark(), packageMark);
		}
		return dataMap;
	}

	public static PackageMark getPackageMark(String packageMark){
		return packageMarkMap.get(packageMark);
	}

	/**
	 * 初始化推荐版本
	 */
	private void initRecommendVersionMap(){
		Map<String, Map<String, PackageVersion>> versionMap = new ConcurrentHashMap<>();
		Map<String, PackageVersion> recommendMap = new ConcurrentHashMap<>();
		Map<String, PackageVersion> whiteDataMap = new ConcurrentHashMap<>();
		Map<String, PackageVersion> forwardDataMap = new ConcurrentHashMap<>();
		for(PackageVersion packageVersion : packageVersionList){
			if(!versionMap.containsKey(packageVersion.getPackageMark())){
				versionMap.put(packageVersion.getPackageMark(), new ConcurrentHashMap<>());
			}
			versionMap.get(packageVersion.getPackageMark()).put(packageVersion.getPackageVersion(), packageVersion);

			if(!packageVersion.isDelete()) {
				if (packageVersion.isRecommendVersion()) {
					recommendMap.put(packageVersion.getPackageMark(), packageVersion);
				}

				if (packageVersion.isWhiteRecommendVersion()) {      //白名单
					whiteDataMap.put(packageVersion.getPackageMark(), packageVersion);
				}

				if(packageVersion.isForwardRecommendVersion()){      //先遣服
					forwardDataMap.put(packageVersion.getPackageMark(), packageVersion);
				}
			}
		}
		packageVersionMap = versionMap;
		recommendPackageVersionMap = recommendMap;
		whiteRecommendPackageVersionMap = whiteDataMap;
		forwardRecommendPackageVersionMap = forwardDataMap;
	}

	/**
	 * 获取包体信息
	 * @param packageMark
	 * @return
	 */
	public static PackageVersion getPackageVersion(String packageMark, String packageVersion){
		Map<String, PackageVersion> dataMap = packageVersionMap.get(packageMark);
		if(null != dataMap){
			return dataMap.get(packageVersion);
		}
		return null;
	}

	/**
	 * 获取推荐版本
	 * @param packageMark
	 * @param isWhiteUser
	 * @return
	 */
	public static PackageVersion getRecommendVersion(String packageMark, boolean isWhiteUser, int forwardServer){
		PackageVersion recommendVersion;
		if(isWhiteUser){//白名单
			recommendVersion = whiteRecommendPackageVersionMap.get(packageMark);
		}else if(forwardServer > 0){//先遣服
			recommendVersion = forwardRecommendPackageVersionMap.get(packageMark);
		}else {
			recommendVersion = recommendPackageVersionMap.get(packageMark);
		}
		return recommendVersion;
	}

	public static List<PackageUpdateInfo> getPackageUpdateInfoList() {
		return new ArrayList<>(packageUpdateInfoList);
	}

	/**
	 * 初始化推荐版本
	 */
	private void initPackageUpdateVersionMap(){
		Map<String, List<PackageUpdateInfo>> dataMap = new ConcurrentHashMap<>();
		for(PackageUpdateInfo updateInfo : getPackageUpdateInfoList()){
			if(updateInfo.isDirtyVer()){//无效的版本号
				continue;
			}
			if(!dataMap.containsKey(updateInfo.getPackageMark())){
				dataMap.put(updateInfo.getPackageMark(), new ArrayList<>());
			}
			List<PackageUpdateInfo> dataList = dataMap.get(updateInfo.getPackageMark());
			dataList.add(updateInfo);
		}
		packageUpdateInfoMap = dataMap;
	}

	/**
	 * 获取推荐版本
	 * @return
	 */
	public static PackageUpdateInfo getPackageUpdateInfo(String packageMark, String packageVersion, boolean isWhiteUser, int forwardServer){
		List<PackageUpdateInfo> dataList = getPackageUpdateInfoList(packageMark);
		if(null == dataList){
			return null;
		}
		PackageUpdateInfo packageUpdateInfo;
		int updateScope;
		if(isWhiteUser){//白名单
			updateScope = eUpdateScope.WhiteUser.getValue();
			packageUpdateInfo = getScopePackageUpdateInfo(updateScope, packageVersion, dataList);
		}else if(forwardServer > 0) {//先遣服
			updateScope = eUpdateScope.ForwardServer.getValue();
			packageUpdateInfo = getScopePackageUpdateInfo(updateScope, packageVersion, dataList);
		}else {//正式服
			updateScope = eUpdateScope.FormalServer.getValue();
			packageUpdateInfo = getScopePackageUpdateInfo(updateScope, packageVersion, dataList);
		}
		return packageUpdateInfo;
	}

	/**
	 * 获取符合条件的更新信息
	 * @param updateScope
	 * @param packageVersion
	 * @param dataList
	 * @return
	 */
	private static PackageUpdateInfo getScopePackageUpdateInfo(int updateScope, String packageVersion, List<PackageUpdateInfo> dataList){
		if(null != dataList){
			for(PackageUpdateInfo data : dataList){
				if(isInPackageVersion(data, packageVersion) && data.getUpdateScopeList().contains(updateScope)){
					return data;
				}
			}
		}
		return null;
	}

	/**
	 * 通过包体标识获取更新信息集合
	 * @param packageMark
	 * @return
	 */
	private static List<PackageUpdateInfo> getPackageUpdateInfoList(String packageMark){
		if(packageUpdateInfoMap.containsKey(packageMark)){
			return new ArrayList<>(packageUpdateInfoMap.get(packageMark));
		}
		return null;
	}

	/**
	 * 时候版本内
	 * @param packageUpdateInfo
	 * @param packageVersion
	 * @return
	 */
	private static boolean isInPackageVersion(PackageUpdateInfo packageUpdateInfo, String packageVersion){
		if(null == packageUpdateInfo){
			return false;
		}
		if(packageUpdateInfo.isDirtyVer()){
			return false;
		}
		if(StringUtils.isNullOrEmpty(packageVersion)){
			return false;
		}
		String[] verArr = packageVersion.split("\\.");
		if(verArr.length < 3){
			return false;
		}
		int v1,v2,v3;
		try {
			v1 = Integer.parseInt(verArr[0]);
			v2 = Integer.parseInt(verArr[1]);
			v3 = Integer.parseInt(verArr[2]);
		}catch (NumberFormatException e){
			logger.error("packageVersion format error,packageVersion:{}", packageVersion);
			return false;
		}

		if(v1 < packageUpdateInfo.getVerMin1()){
			return false;
		}else if(v1 == packageUpdateInfo.getVerMin1()){
			if(v2 < packageUpdateInfo.getVerMin2()){
				return false;
			}else if(v2 == packageUpdateInfo.getVerMin2()){
				if(v3 < packageUpdateInfo.getVerMin3()){
					return false;
				}
			}
		}

		if(v1 > packageUpdateInfo.getVerMax1()){
			return false;
		}else if(v1 == packageUpdateInfo.getVerMax1()){
			if(v2 > packageUpdateInfo.getVerMax2()){
				return false;
			}else if(v2 == packageUpdateInfo.getVerMax2()){
				if(v3 > packageUpdateInfo.getVerMax3()){
					return false;
				}
			}
		}
		return true;
	}
}
